#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: V1Dag.py 
@version:
@time: 2022/03/10 
"""
from TapClientCore.T04_logic.Dag02.relations.relation import TaskMapTable


class V1DagDataCopy:

    def __init__(self):
        # dag
        from TapClientCore.T03_api_body_templates.v2_api_body import TASK_CREATE_DATA_COPY_DAG
        from TapClientCore.T03_api_body_templates.v2_api_body import TASK_CREATE_DATA_COPY_DAG_EDGES
        self.dag = TASK_CREATE_DATA_COPY_DAG.copy()
        self.edges = TASK_CREATE_DATA_COPY_DAG_EDGES.copy()
        self.nodes = []
        # attrs
        from TapClientCore.T03_api_body_templates.v2_api_body import TASK_CREATE_DATA_COPY_ATTRS
        from TapClientCore.T03_api_body_templates.v2_api_body import TASK_CREATE_DATA_COPY_ATTRS_ATTRS_TASK_SOURCE_DATA
        from TapClientCore.T03_api_body_templates.v2_api_body import TASK_CREATE_DATA_COPY_ATTRS_ATTRS_TASK_SETTING_DATA
        self.attrs = TASK_CREATE_DATA_COPY_ATTRS.copy()
        self.source_data = TASK_CREATE_DATA_COPY_ATTRS_ATTRS_TASK_SOURCE_DATA.copy()
        self.source_setting = TASK_CREATE_DATA_COPY_ATTRS_ATTRS_TASK_SETTING_DATA.copy()

    def edge(self, source, sink, relation: TaskMapTable, setting):
        # dag create
        self.edges.update({'source': source.node.get('id')})
        self.edges.update({'target': sink.node.get('id')})
        # sink update
        sink_append_attr = {
            'syncObjects': [
                relation.syncObjects
            ],
            'tablePrefix': relation.tablePrefix,
            'tableSuffix': relation.tableSuffix,
            'tableNameTransform': relation.tableNameTransform,
            'fieldsNameTransform': relation.fieldsNameTransform,
            'batchOperationList': relation.batchOperationList
        }
        sink.node.update(sink_append_attr)
        # source and sink update
        from TapClientCore.T03_api_body_templates.v2_api_body import TASK_CREATE_DATA_COPY_DAG_NODE_SOURCE
        source_template = TASK_CREATE_DATA_COPY_DAG_NODE_SOURCE.copy()
        from TapClientCore.T03_api_body_templates.v2_api_body import TASK_CREATE_DATA_COPY_DAG_NODE_TARGET
        sink_template = TASK_CREATE_DATA_COPY_DAG_NODE_TARGET.copy()
        source_template.update(source.node)
        sink_template.update(sink.node)
        self.nodes.append(source_template)
        self.nodes.append(sink_template)
        self.dag['edges'] = [self.edges]
        self.dag['nodes'] = self.nodes
        # attrs create
        self.source_data = {
            "source_sourceType": "",
            "target_targetType": "",
            "source_filter_databaseType": source.node.get('databaseType'),
            "target_filter_databaseType": sink.node.get('databaseType'),
            "source_connectionId": source.node.get('connectionId'),
            "target_connectionId": sink.node.get('connectionId'),
            "source_connectionName": source.node.get('name'),
            "target_connectionName": sink.node.get('name'),
            "source_databaseType": source.node.get('databaseType'),
            "target_databaseType": sink.node.get('databaseType'),
            "mqType": ""
        }
        self.source_setting = setting
        self.attrs['task_data_source_Data'] = self.source_data
        self.attrs['task_setting_Data'] = self.source_setting


if __name__ == '__main__':
    from pprint import pprint
    from TapClientCore.T04_logic.Dag02.node.data_copy_source_node import DataCopySourceNode
    from TapClientCore.T04_logic.Dag02.node.data_copy_target_node import DataCopySinkNode

    S = DataCopySourceNode('auto_mysql')
    T = DataCopySinkNode('auto_mysql')
    d = V1DagDataCopy()
    d.edge(S, T, TaskMapTable({'table': 'my2'}), {})
    pprint(d.dag)
    pprint(d.attrs)
